package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.condition.ProConditionHelp;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormLinkBusinessHandlerImpl extends ConditionFormLinkDataHandlerImpl {


    private static final Logger LOGGER = LoggerFactory.getLogger(ConditionFormLinkBusinessHandlerImpl.class);


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.LINK_BUSINESS_SINGLE.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        Object object = data.getLong(fieldAttrEntity.getAttr());
        businessRuleInfoPojo.setObject(object);
        return object;
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        Object object = businessRuleInfoPojo.getObject();
        object = ProConditionHelp.getIdByLinkBusiness(object);
        return object;
    }



    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        return ProConditionHelp.getIdByLinkBusiness(value);
    }


    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return handlerFixedValue(thisField, businessRuleInfoPojo, filterCondition);
    }
}
